using System;
using BitMango.Diagnostics;
using Holoville.HOTween;
using UnityEngine;

public class BoxWidgetHandler : MonoBehaviour, IWidget, IHandlerBase
{
	private void Update()
	{
		if (!this.isClear)
		{
			return;
		}
		if (this.time >= 0.6f)
		{
			this.isClear = false;
			this.time = 0f;
			this.ren.material.SetFloat("_WhiteColor", 0f);
			return;
		}
		if (!this.isChangeColor && this.time >= 0.29f)
		{
			this.isChangeColor = true;
			if (this.changeColor > -1 && this.changeColor < 255)
			{
				this.SetMesh(this.changeColor);
			}
		}
		this.ren.material.SetFloat("_WhiteColor", Mathf.Abs(0.3f - Mathf.Abs(this.time - 0.3f)) * 1.67f);
		this.time += Time.deltaTime;
	}

	public void SetData(int index, bool _isUp, int _tangramIndex, BlockType _blockType)
	{
		Debugger.Assert(index >= 0, "BoxWidgetHandler index >= 0");
		HOTween.Complete(base.gameObject.transform);
		base.transform.eulerAngles = Vector3.zero;
		this.time = 0f;
		this.rotation = false;
		this.isClear = false;
		this.isChangeColor = false;
		this.tangramIndex = _tangramIndex;
		this.isUp = _isUp;
		this.blockType = _blockType;
		if (this.blockType == BlockType.NORMAL || this.blockType == BlockType.INIT)
		{
			if (this.subBoxWidget != null)
			{
				this.subBoxWidget.SetData(this.isUp, this.tangramIndex, this.blockType);
			}
			else
			{
				this.subBoxWidget = Widget.Create<SubBoxWidgetHandler>();
				this.subBoxWidget.transform.parent = base.transform;
				this.subBoxWidget.transform.localScale = Vector3.one;
				this.subBoxWidget.transform.localPosition = Vector3.zero;
				this.subBoxWidget.SetData(this.isUp, this.tangramIndex, this.blockType);
			}
		}
		if (this.blockType == BlockType.SHADOW)
		{
			Color color = this.sprite.color;
			color.a = 0.4f;
			this.sprite.color = color;
		}
		if (this.blockType == BlockType.INIT)
		{
			this.sprite.color = new Color(1f, 1f, 1f, 1f);
			if (GameMode.gameMode == GameType.TRIANGLE)
			{
				this.sprite.spriteName = ((!this.isUp) ? "background_triangle_1" : "background_triangle_0");
			}
			else if (GameMode.gameMode == GameType.SQUARE)
			{
				this.sprite.spriteName = "background_square";
			}
			else if (GameMode.gameMode == GameType.HEXA)
			{
				this.sprite.spriteName = "background_hexa";
			}
			else if (GameMode.gameMode == GameType.RHOMBUS)
			{
				this.sprite.spriteName = "background_rhombus";
			}
			else if (GameMode.gameMode == GameType.TANGRAM)
			{
				this.sprite.spriteName = string.Format("background_tangram_{0}", this.tangramIndex);
			}
		}
		else if (GameMode.gameMode == GameType.TRIANGLE)
		{
			this.sprite.spriteName = ((!this.isUp) ? "block_triangle_1" : "block_triangle_0");
		}
		else if (GameMode.gameMode == GameType.SQUARE)
		{
			this.sprite.spriteName = "block_square";
		}
		else if (GameMode.gameMode == GameType.HEXA)
		{
			this.sprite.spriteName = "block_hexa";
		}
		else if (GameMode.gameMode == GameType.RHOMBUS)
		{
			this.sprite.spriteName = "block_rhombus";
		}
		else if (GameMode.gameMode == GameType.TANGRAM)
		{
			this.sprite.spriteName = string.Format("block_tangram_{0}", this.tangramIndex);
		}
		this.SetMesh(index);
		this.DoDataExchange();
	}

	private void SetMesh(int index)
	{
		if (this.blockType == BlockType.INIT)
		{
			this.ren.material.SetFloat("_OriginColor", 1f);
			this.ren.material.SetFloat("_MultiColor", 0f);
		}
		else if (index > -1 && index < 255)
		{
			MultiColorExt.SetColor(this.filter, index);
		}
	}

	public void DoDataExchange()
	{
		if (!base.gameObject.CompareTag("_box"))
		{
			this.boxCollider.size = Vector3.zero;
		}
	}

	public void SetOutline(bool isSet)
	{
		if (this.blockType != BlockType.NORMAL)
		{
			return;
		}
		this.subBoxWidget.SetOutline(isSet);
	}

	public void ClearAnimation()
	{
		if (this.blockType != BlockType.NORMAL)
		{
			return;
		}
		this.isClear = true;
	}

	public MeshFilter filter;

	public tk2dSprite sprite;

	public BoxCollider boxCollider;

	public Renderer ren;

	private float time;

	public bool isClear;

	private bool rotation;

	public bool isUp;

	public int tangramIndex;

	private BlockType blockType;

	private SubBoxWidgetHandler subBoxWidget;

	private bool isChangeColor;

	public int changeColor = -1;
}
